package org.adream.account.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.adream.account.model.ResultModel;
import org.adream.account.service.SolrSearchService;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

/**
 * solr搜索应用
 *
 */
@Controller
@RequestMapping(value = "/solr")
public class SolrSearchController {

	@Autowired
	SolrSearchService service;

	/**
	 * 搜索   库：solr_core
	 * 固定每次搜索前20条 无分页
	 * @param json json字符串 包含参数 where
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "search", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<?> search(@RequestBody String json, HttpServletRequest request) {
		JSONObject jsonObject = JSONObject.parseObject(json);
		String where = jsonObject.getString("where").trim();
		if (StringUtils.isEmpty(where)) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, ResultModel.ERR_PARAM_MSG, null);
		}
		SolrDocumentList results = service.solrSearch(where);
		return new ResultModel<SolrDocumentList>(ResultModel.SUCCESS, "搜索成功", results);
	}
	/**
	 * 学校搜索（全部）库：school
	 * 固定每次搜索前20条 无分页
	 * @param json
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "schoolSearch", produces = "application/json;charset=UTF-8" ,method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<?> schoolSearch(@RequestParam(required = false) String text,@RequestParam(required = false) String page, HttpServletRequest request) {
		if(text==null){
			text="";
		}
		text=text.trim();
		SolrDocumentList results = service.schoolSearch(text);
		return new ResultModel<SolrDocumentList>(ResultModel.SUCCESS, "搜索成功", results);
	}
	
	/**
	 * 学校搜索（梦想中心）库：school
	 * 固定每次搜索前20条 无分页
	 * @param json
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "adreamCenterSearch", produces = "application/json;charset=UTF-8" ,method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<?> adreamCenterSearch(@RequestParam(required = false) String text,@RequestParam(required = false) String page, HttpServletRequest request) {
		if(text==null){
			text="";
		}
		text=text.trim();
		SolrDocumentList results = service.adreamCenterSearch(text);
		return new ResultModel<SolrDocumentList>(ResultModel.SUCCESS, "搜索成功", results);
	}
	
	/**
	 * 高亮搜索 库：school
	 * 固定每次搜索前20条 无分页
	 * @param json
	 * @return
	 */
	@RequestMapping(value = "highlightSearch", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<?> highlightSearch(@RequestBody String json) {
		JSONObject jsonObject = JSONObject.parseObject(json);
		String searchValue = jsonObject.getString("searchValue").trim();
		if(StringUtils.isEmpty(searchValue)) {
			return new ResultModel<String>(ResultModel.SUCCESS,"搜索条件空",null);
		}
		String time = jsonObject.getString("time").trim();
		if(StringUtils.isEmpty(time)||"null".equals(time)) {
			time = null;
		}
		String source = jsonObject.getString("source").trim();
		if(StringUtils.isEmpty(source)||"null".equals(source)) {
			source = null;
		}
		String sort = jsonObject.getString("sort").trim();
		if(StringUtils.isEmpty(sort)||"null".equals(sort)) {
			sort = null;
		}
		Integer pageNo = 0;
		Integer pageSize = 10;
		try {
			pageNo = Integer.parseInt(jsonObject.getString("pageNo").trim());
			pageSize = Integer.parseInt(jsonObject.getString("pageSize").trim());
		} catch (Exception e) {
			 
		}
		Map<String, Object> result = service.highlightSearch(searchValue,source,time,sort,pageNo,pageSize);
		return new ResultModel<Map<String, Object>>(ResultModel.SUCCESS, "搜索成功", result);
	}
	
	/**
	 * 学校搜索 库：school_basic
	 * 固定每次搜索前20条 无分页
	 * @param json
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "schoolBasicSearch", produces = "application/json;charset=UTF-8" ,method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<?> schoolBasicSearch(@RequestParam(required = false) String text,@RequestParam(required = false) String page, HttpServletRequest request) {
		if(text==null){
			text="";
		}
		text=text.trim();
		SolrDocumentList results = service.schoolBasicSearch(text);
		return new ResultModel<SolrDocumentList>(ResultModel.SUCCESS, "搜索成功", results);
	}
}